package c926.xiaoduyun.xiaodu.plugin.other;

import kd.bos.dataentity.entity.DynamicObject;

import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.entity.MulBasedataDynamicObjectCollection;
import kd.bos.ext.form.control.CustomControl;
import kd.bos.form.control.Button;
import kd.bos.form.control.Control;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.control.events.BeforeClickEvent;
import kd.bos.form.control.events.BeforeItemClickEvent;
import kd.bos.form.events.CustomEventArgs;
import kd.bos.form.field.AttachmentEdit;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.list.BillList;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.attachment.AttachmentFieldServiceHelper;
import kd.bos.url.UrlService;
import kd.sdk.plugin.Plugin;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;


/**
 * 动态表单插件
 */
public class MyPDFSealPlugin extends AbstractFormPlugin implements Plugin {


    @Override
    public void customEvent(CustomEventArgs e) {
        String eventName = e.getEventName();
        if ("getAttachmentUrl".equals(eventName)) {

            CustomControl customcontrol = this.getView().getControl("c926_customcontrolap1");
            Map<String, Object> paramsMap = new HashMap<>();
            String url = "";
            DynamicObjectCollection entry = this.getModel().getEntryEntity("c926_entryentity");
            for (DynamicObject row : entry) {
                DynamicObjectCollection atts = row.getDynamicObjectCollection("c926_attachmentfield");
                for (DynamicObject att : atts) {
                    long attid = att.getLong("fbasedataid_id");
                    DynamicObject attachment = QueryServiceHelper.queryOne("bd_attachment", "name,url,type", new QFilter("id", QCP.equals, attid).toArray());
                    url = attachment.getString("url");
                    String filetype = attachment.getString("type");
                    String fullUrl = UrlService.getAttachmentPreviewUrl(url);
                    // 检查文件类型是否为PDF
                    paramsMap.put("fileUrl", url);
                    paramsMap.put("filetype", filetype);
                    break;
                }
            }
            customcontrol.setData(paramsMap);
        }
        super.customEvent(e);
    }
}

